package com.mtw.bbs.like.service;



import com.mtw.bbs.like.pojo.dto.LikeCountDto;
import com.mtw.bbs.like.pojo.dto.LikeRecordDto;
import com.mtw.bbs.article.pojo.vo.LikeCountVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 *
 */
@Service
@RequiredArgsConstructor
public class LikeProviderService {


    private final LikeCountService likeCountService;
    private final LikeRecordService likeRecordService;


    /**
     * 根据用户id，业务id和实体id判断用户是否已点赞
     * @param objectType    业务id
     * @param objectId      实体id
     * @param userMid       用户id
     */
    public LikeCountVo getLiked(String objectType, String objectId,String userMid) {


        LikeRecordDto dto = new LikeRecordDto();
        dto.setObjectId(objectId);
        dto.setUserMid(userMid);
        dto.setObjectType(objectType);
        int liked = likeRecordService.userIsLiked(dto);
        LikeCountVo vo = new LikeCountVo();
        vo.setObjectId(objectId);
        vo.setLikeStatus(liked);
        return vo;
    }


    /**
     * 根据用户id，业务id和实体id集合判断用户是否已点赞
     * @param objectType    业务id
     * @param objectIds     实体id集合
     * @param userMid       用户id
     */
    public List<LikeCountVo> getLikedList(String objectType, List<String> objectIds,String userMid) {

        return objectIds.stream().map(x -> {
            LikeRecordDto dto = new LikeRecordDto();
            dto.setObjectId(x);
            dto.setUserMid(userMid);
            dto.setObjectType(objectType);
            int r = likeRecordService.userIsLiked(dto);
            LikeCountVo vo = new LikeCountVo();
            vo.setLikeStatus(r);
            vo.setObjectId(x);
            return vo;
        }).toList();
    }

    /**
     * 根据用户id，业务id和实体id判断用户是否已点赞，并带出点赞量
     * @param objectType    业务id
     * @param objectId      实体id
     * @param userMid       用户id
     */
    public LikeCountVo getLikedWithCount(String objectType, String objectId,String userMid) {

        LikeCountVo vo;
        // 是否已点赞
        if (StringUtils.isBlank(userMid)) {
            vo = new LikeCountVo();
            vo.setObjectId(objectId);
            vo.setLikeStatus(0);
        } else {
            vo = this.getLiked(objectType,objectId,userMid);
        }
        // 点赞数量
        LikeCountVo countVo = this.getCount(objectType,objectId);
        vo.setCount(countVo == null || countVo.getCount() == null ? 0 : countVo.getCount());
        return vo;
    }


    /**
     * 根据用户id，业务id和实体id集合判断用户是否已点赞，并带出点赞量
     */
    public List<LikeCountVo> getLikedListWithCount(String objectType, List<String> objectIds,String userMid) {


        List<LikeCountVo> vos;

        // 是否已点赞
        if (StringUtils.isBlank(userMid)) {
            vos = objectIds.stream().map(x -> {
                LikeCountVo vo = new LikeCountVo();
                vo.setObjectId(objectType);
                vo.setLikeStatus(0);
                return vo;
            }).toList();
        } else {
            vos = getLikedList(objectType,objectIds,userMid);
        }
        // 点赞总数
        for (LikeCountVo vo : vos) {
            LikeCountDto dto = new LikeCountDto();
            dto.setObjectId(vo.getObjectId());
            dto.setObjectType(objectType);
            Integer likeCount = likeCountService.getLikeCount(dto);
            vo.setCount(likeCount == null ? 0 : likeCount);
        }
        return vos;
    }


    /**
     * 根据用业务id和实体id获取点赞数量
     * @param objectType    业务id
     * @param objectId      实体id
     */
    public LikeCountVo getCount(String objectType, String objectId) {

        LikeCountDto dto = new LikeCountDto();
        dto.setObjectType(objectType);
        dto.setObjectId(objectId);
        Integer likeCount = likeCountService.getLikeCount(dto);
        return  new LikeCountVo(objectId, likeCount);
    }


    /**
     * 根据用业务id和实体id集合获取点赞数量
     * @param objectType    业务id
     * @param objectIds     实体id集合
     */
    public List<LikeCountVo> getCountList(String objectType, List<String> objectIds) {

        return objectIds.stream().map(x -> {
            LikeCountDto dto = new LikeCountDto();
            dto.setObjectId(x);
            dto.setObjectType(objectType);
            Integer likeCount = likeCountService.getLikeCount(dto);
            return  new LikeCountVo(x, likeCount);
        }).toList();
    }


    /**
     * 根据用业务id和用户id获取已点对象id
     * @param userId        用户id
     * @param page          当前页
     * @param size          分页大小
     * @param objectType    业务对象
     */
    public List<String> getUserLikeList(String userId,int page,int size,String objectType) {

        LikeRecordDto dto = new LikeRecordDto();
        dto.setUserMid(userId);
        dto.setObjectType(objectType);
        return likeRecordService.getLikedListByUser(page, size, dto);
    }

}
